//
//  UIPullingheaderTableView.m
//  collection
//
//  Created by xinggang li on 1/16/12.
//  Copyright (c) 2012 tencent. All rights reserved.
//

#import "UIPullingheaderTableView.h"

@implementation UIPullingheaderTableView

//@synthesize secondDelegate = _secondDelegate;
@synthesize timeStamp;
@synthesize forbidLoading;

-(void)setTimeStampToDate:(NSDate *)dateValue
{
    if(nil == dateValue)
    {
        dateValue = [NSDate date];
    }
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"信息更新于 :MM/dd HH:mm:ss"];
    self.timeStamp = [dateFormat stringFromDate:dateValue];
    [dateFormat release];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        // self.delegate = self;
        self.forbidLoading = NO;
        
        _refreshView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, -360, 320, 360)];
        _refreshView.delegate = self;
        
        [self addSubview:_refreshView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        // Initialization code
        // self.delegate = self;
        self.forbidLoading = NO;
        
        _refreshView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, -360, 320, 360)];
        _refreshView.delegate = self;
        
        [self addSubview:_refreshView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        // self.delegate = self;
        self.forbidLoading = NO;
        
        _refreshView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, -360, 320, 360)];
        _refreshView.delegate = self;
        
        [self addSubview:_refreshView];
    }
    return self;
}

-(void)dealloc
{
    [_refreshView release];
    self.timeStamp = nil;
    [super dealloc];
}


-(void)setDelegate:(id<UITableViewDelegate,UIPullingheaderDelegate>)delegate
{
    super.delegate = self;
    
    _secondDelegate = delegate;
}

-(BOOL)isUpdatingData
{
    if ([_refreshView getPullingState] == EGOOPullRefreshLoading) 
    {
        return YES;
    }
    
    return NO;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


-(void)delayFinishedRefreshLoading
{
    [_refreshView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
}


-(void)finishedRefreshLoading:(NSTimeInterval) delay;
{
    if ( delay < 0.001f )
    {
        [_refreshView egoRefreshScrollViewDataSourceDidFinishedLoading:self];
    }
    else
    {
        [NSTimer scheduledTimerWithTimeInterval:delay
                                       target:self 
                                     selector:@selector(delayFinishedRefreshLoading)
                                     userInfo:nil
                                      repeats:NO];
    }
}





- (void)refreshLastUpdatedDate
{
    [_refreshView refreshLastUpdatedDate];
}

-(void)enforceRefreshLoading
{
    [_refreshView egoEnforceLoading:self];
}


//////////////////////////////////////////////
//
//      EGORefreshTableHeaderDelegate 
//
//////////////////////////////////////////////


- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
    [_secondDelegate onInvokePullingRefresh:self];
}


- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view
{
    return self.forbidLoading;
}


- (NSString*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view
{
    if ( self.timeStamp && [self.timeStamp length] > 0 )
        return self.timeStamp;
    else {
        NSDate* date = [NSDate date];
        return [NSString stringWithFormat:@"信息更新于:%@", date.description];
    }
        //[[CConfiguration sharedConfiguration] getDateNowString:@"MM/dd HH:mm:ss"]];
}



//////////////////////////////////////////////
//
//      UIScrollViewDelegate 
//
//////////////////////////////////////////////

#pragma mark - protocol UIScrollViewDelegate 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{    
    [_refreshView egoRefreshScrollViewDidScroll:scrollView];
    
    if ( [_secondDelegate respondsToSelector:@selector(scrollViewDidScroll:)] ){
        
        [_secondDelegate scrollViewDidScroll:scrollView];
    }
}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{    
    [_refreshView egoRefreshScrollViewDidEndDragging:scrollView];
    
    if ( [_secondDelegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)] ){
        
        [_secondDelegate scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
}



//////////////////////////////////////////////
//
//      UITableViewDelegate 
//
//////////////////////////////////////////////
#pragma mark - protocol UITableViewDelegate 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:willDisplayCell:forRowAtIndexPath:)] ){
    
        [_secondDelegate tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
    }
}

// Variable height support

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:heightForRowAtIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView heightForRowAtIndexPath:indexPath];
    }
    
    return 0.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)] )
    {
        return [_secondDelegate tableView:tableView heightForHeaderInSection:section];
    }
    
    return 0.0f;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:heightForFooterInSection:)] )
    {
        return [_secondDelegate tableView:tableView heightForFooterInSection:section];
    }
    
    return 0.0f;
}

// Section header & footer information. Views are preferred over title should you decide to provide both

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)] )
    {
        return [_secondDelegate tableView:tableView viewForHeaderInSection:section];
    }
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:viewForFooterInSection:)] )
    {
        return [_secondDelegate tableView:tableView viewForFooterInSection:section];
    }
    
    return nil;
}

// Accessories (disclosures). 
/*
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:accessoryTypeForRowWithIndexPath:)] )
    {
        [_secondDelegate tableView:tableView accessoryTypeForRowWithIndexPath:indexPath];
    }
    
    return UITableViewCellAccessoryDisclosureIndicator;
}
*/

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:accessoryButtonTappedForRowWithIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
    }
}


// Selection

// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:willSelectRowAtIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView willSelectRowAtIndexPath:indexPath];
    }
    
    return indexPath;
}


- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:willDeselectRowAtIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView willDeselectRowAtIndexPath:indexPath];
    }
    
    return indexPath;
}


// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)] )
    {
        [_secondDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:didDeselectRowAtIndexPath:)] )
    {
        [_secondDelegate tableView:tableView didDeselectRowAtIndexPath:indexPath];
    }
}


// Editing

// Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:editingStyleForRowAtIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView editingStyleForRowAtIndexPath:indexPath];
    }
    
    return UITableViewCellEditingStyleNone;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView titleForDeleteConfirmationButtonForRowAtIndexPath:indexPath];
    }

    return @"";
}


// Controls whether the background is indented while editing.  If not implemented, the default is YES.  This is unrelated to the indentation level below.  This method only applies to grouped style table views.
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:shouldIndentWhileEditingRowAtIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView shouldIndentWhileEditingRowAtIndexPath:indexPath];
    }

    return YES;
}


// The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:willBeginEditingRowAtIndexPath:)] )
    {
        [_secondDelegate tableView:tableView willBeginEditingRowAtIndexPath:indexPath];
    }
}


- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:didEndEditingRowAtIndexPath:)] )
    {
        [_secondDelegate tableView:tableView didEndEditingRowAtIndexPath:indexPath];
    }
}


// Moving/reordering

// Allows customization of the target row for a particular row as it is being moved/reordered
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:)] )
    {
        [_secondDelegate tableView:tableView targetIndexPathForMoveFromRowAtIndexPath:sourceIndexPath toProposedIndexPath:proposedDestinationIndexPath];
    }
    
    // is it right??
    return proposedDestinationIndexPath;
}

// Indentation

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath // return 'depth' of row for hierarchies
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:indentationLevelForRowAtIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
    }
    
    // is it right?
    return indexPath.row;
}


// Copy/Paste.  All three methods must be implemented by the delegate.

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:shouldShowMenuForRowAtIndexPath:)] )
    {
        return [_secondDelegate tableView:tableView shouldShowMenuForRowAtIndexPath:indexPath];
    }
    
    return NO;
}


- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:canPerformAction:forRowAtIndexPath:withSender:)] )
    {
        return [_secondDelegate tableView:tableView canPerformAction:action forRowAtIndexPath:indexPath withSender:sender];
    }
    
    return NO;
}


- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if ( [_secondDelegate respondsToSelector:@selector(tableView:performAction:forRowAtIndexPath:withSender:)] )
    {
        [_secondDelegate tableView:tableView performAction:action forRowAtIndexPath:indexPath withSender:sender];
    }
}

//////////////////////////////////////////////
//
//      UIScrollViewDelegate 
//
//////////////////////////////////////////////

@end
